package com.thinkwide.data.net;

import com.thinkwide.data.core.MFString;
import com.thinkwide.data.debug.MFDebug;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class MFHtmlParse {

    public static void replace(Element el, String html) {
        if (el == null) {
            return;
        }
        Element e = el.nextElementSibling();
        if (e != null) {
            e.before(html);
        } else if (el.parent() != null) {
            el.parent().append(html);
        }
        el.remove();
    }

    public String analyze(String html, String cssQuery) {
        Document doc = Jsoup.parse(html);
        Elements els = doc.select("input[type=text]");
        int i = 0;
        for (Element item : els) {
            MFDebug.println(item.attr("id"));

            i++;
            if (MFString.compareCase(item.attr("id"), "3")) {
                MFDebug.println("ok");
                Element e = item.nextElementSibling();
                if (e != null) {
                    e.before(String.format("<xx>%s</xx>", i));
                } else {
                    item.parent().append(String.format("<xx>%s</xx>", i));
                }
                item.remove();
            }

        }

        return doc.toString();
    }
}
